package ModelLayer;

import java.util.*;
public class ProductContainer
{
    private ArrayList<Product>  productContainer;
    private static ProductContainer instance;

    private ProductContainer()
    {
        productContainer = new  ArrayList<Product>();
    }

    public static ProductContainer getInstance(){
        if (instance == null){
            instance = new ProductContainer();
        }
        return instance;
    }

    public Product findProductByName(String name){
        Product pObject = null;
        int index = 0;
        boolean found = false;
        while(index < productContainer.size() && !found)
        {
            pObject = productContainer.get(index); 
            if(pObject.getName().equals(name))
            {
                found = true;
            }
            else{
                index++;
            }
        }

        if(found){
            return productContainer.get(index) ;
        }else{
            return  null;
        }
    }

    public Product findProductByBarcode(int barcode){
        Product pObject = null;
        int index = 0;
        boolean found = false;
        while(index < productContainer.size() && !found)
        {
            pObject = productContainer.get(index); 
            if(pObject.getBarcode() == barcode)
            {
                found = true;
            }
            else{
                index++;
            }
        }
        if(found){
            return productContainer.get(index) ;
        }else{
            return  null;
        }
    }

    public void addProduct(Product p){
        productContainer.add(p);
    }

    public void deleteProduct(Product p){
        if(productContainer.contains(p))
            productContainer.remove(p);
    }
    
    public ArrayList<Product> getProductsList()
    {
        return productContainer;
    }
}
